<template>
  <div class="yy-schedule-setting">
    <el-dialog
        v-model="isVisible"
        title="改签患者"
        width="90%"
        :close-on-click-modal="false"
        @close="onCancelDialog"
        :show-close="true"
        class="yy-modal--custom"
        style="max-width: 500px;"
    >
      <div>
        <el-select v-model="selectDoctorId"  placeholder="请选择医生" style="min-width: 120px; flex:1;">
          <el-option :disabled="item.flowFlag === 0" :label="item.doctorName + `(${item.doctorCode})`" :value="item.doctorId" v-for="item in schedules" :key="item.doctorId">
            <span style="float: left">{{ item.doctorName + `(${item.doctorCode})` }}</span>
            <span style="float: right;"> 候诊: {{ item.awaitCount }} </span>
          </el-option>
        </el-select>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancelDialog">关闭</el-button>
          <el-button type="primary" @click="onSave">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import {defineEmits, defineExpose, ref} from "vue";
import {QueueScheduleModel} from "@/model/QueueScheduleModel";
import DoctorClientApi from "@/api/doctor";
import {QueueItemModel} from "@/model/QueueItemModel";

  const $emit = defineEmits({
    'changeSignSuccess': () => true
  })
  const isVisible = ref(false)

  const queueItem = ref()
  const schedules = ref([])
  const currentEmployee = ref()
  const currentSchedule = ref<QueueScheduleModel>()

  const selectDoctorId = ref()

  const onShowDialog = (employee, schedule: QueueScheduleModel, queue: QueueItemModel) => {
    queueItem.value = queue
    currentEmployee.value = employee
    currentSchedule.value = schedule
    listSchedule()
    isVisible.value = true
  }

  const listSchedule = () => {
    DoctorClientApi.listCurrentSchedule(currentEmployee.value.emplId, currentSchedule.value.itemId, currentSchedule.value.departId).then(res =>{
      schedules.value = res.filter(e => e.doctorId != currentEmployee.value.emplId)
    })
  }

  const onSave = () => {
    DoctorClientApi.changeSignQueueItem(currentEmployee.value.emplId, currentSchedule.value.itemId, currentSchedule.value.departId,
        currentSchedule.value.categoryId, selectDoctorId.value, queueItem.value.id)
        .then((res)=>{
          $emit('changeSignSuccess')
          onCancelDialog()
      })
  }

  const onCancelDialog = () => {
    if (isVisible.value) {
      selectDoctorId.value = ""
      currentEmployee.value = null
      currentSchedule.value = null
    }
    isVisible.value = false
  }

  defineExpose({
    onShowDialog
  })

</script>
<style scoped lang="scss">

</style>
